<template>
  <div class="chat">
    <!-- 注册表单 -->
    <div class="register-form-container">
      <el-form
        ref="dom"
        :model="registerForm"
        :rules="registerFormRules"
        label-width="85px"
        class="register-form"
      >
        <h1 style="text-align: center">ChatCove</h1>
        <!-- prop绑定规则 -->
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="registerForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            placeholder="请确认密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="注册身份" prop="userType">
          <el-radio-group v-model="registerForm.userType" class="ml-4">
            <el-radio label="0" size="large"> 普通用户</el-radio>
            <el-radio label="1" size="large">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-show="registerForm.userType == '1'"
          label="管理员密钥"
          prop="adminKey"
        >
          <el-input
            v-model="registerForm.adminKey"
            placeholder="请输入管理员密钥"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import api from "@/api/service";
import { ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
const dom = ref(null); //创建一个dom的响应式对象，通过表单的ref属性让dom成为表单的实例化对象
//组合式api使用router
const router = useRouter();
const registerForm = reactive({
  userName: "",
  password: "",
  confirmPassword: "",
  adminKey: "", //如果用户选择管理员身份注册，则需要一个管理员密钥
  userType: "0", //用户注册的身份选择 0为普通用户，1为管理员
});
//表单验证规则
const registerFormRules = ref({
  userName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, message: "密码长度不能少于 6 位", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: "请确认密码", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请再次输入密码"));
        } else if (value !== registerForm.password) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
});
const register = () => {
  dom.value.validate((valid) => {
    if (valid)
      api.register(registerForm).then((resp) => {
        if (resp.includes("注册失败")) ElMessageBox.alert("注册失败");
        else router.push('/login');
      });
  });
};
</script>
<style scoped>
@keyframes chat {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
/* 聊天页面样式 */
.chat {
  display: flex;
  flex: 1;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(to right, #5fc3e4 0%, #e55d87 100%);
  height: 100vh;
  animation: chat 1s; /* 和上面对应 */
}

.register-form-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60vh;
}

.register-form {
  width: 400px;
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 5px;
  background-color: #ffffff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.register-form-item {
  margin-bottom: 20px;
}

.register-form-button {
  width: 100%;
}
</style>
  

